﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;


namespace MyWeb.Admin
{
    public partial class GroupNews : System.Web.UI.Page
    {
        static string Id = "";
        static bool Insert = false;
        static string Level = "";
        private string Lang = "";
        static string anh_to;
        static string anh_nho;
        static List<ObjLoaiTin> lstObjLoaiTin;
        protected void Page_Load(object sender, EventArgs e)
        {
            //config cke
            //cke_mo_ta_ngan.config.toolbar = new object[]
            //{
            //    new object[] { "Bold", "Italic", "Underline", "Strike", "-", "Subscript", "Superscript" },
            //    new object[] { "NumberedList", "BulletedList", "-", "Outdent", "Indent", "Blockquote", "CreateDiv" },
            //    new object[] { "JustifyLeft", "JustifyCenter", "JustifyRight", "JustifyBlock" },
            //    //new object[] { "BidiLtr", "BidiRtl" },
				
            //    new object[] { "Styles", "Format", "Font", "FontSize" },
				
            //};
            CKFinder.FileBrowser a = new CKFinder.FileBrowser();
            a.SetupCKEditor(cke_noi_dung);


            drop_loai_tin.SelectedIndexChanged += new EventHandler(drop_loai_tin_SelectedIndexChanged);
            drop_lt.SelectedIndexChanged += new EventHandler(drop_lt_SelectedIndexChanged);
            grid_tin.ItemCommand += new DataGridCommandEventHandler(grid_tin_ItemCommand);
            if (!IsPostBack)
            {
                //lay all loai tin
                LoaiTin loaiTin = new LoaiTin();
                lstObjLoaiTin = loaiTin.getLoaiTinAll();
                drop_loai_tin.DataSource = lstObjLoaiTin;
                drop_loai_tin.DataTextField = "ten_loai_tin";
                drop_loai_tin.DataValueField = "loai_tin_id";
                drop_loai_tin.DataBind();
                BindGrid();

            }
        }

        void grid_tin_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            
            string strCA = e.CommandArgument.ToString();
            Id = strCA;
            //loi
            //Response.Write(Id);

            Tin tin = new Tin();
            ObjTin objTin = new ObjTin();
            switch (e.CommandName)
            {
                case "Edit":
                    Insert = false;
                    pnView.Visible = false;
                    pnUpdate.Visible = true;
                    drop_lt.DataSource = lstObjLoaiTin;
                    drop_lt.DataTextField = "ten_loai_tin";
                    drop_lt.DataValueField = "loai_tin_id";
                    drop_lt.DataBind();
                    
                    
                    //lay chi tiet tin
                    objTin = tin.getTinByTinId(strCA);
                    txt_tieu_de.Text = objTin.Tieu_de;
                    cke_mo_ta_ngan.Text = objTin.Tom_tat;
                    cke_noi_dung.Text = objTin.Noi_dung;
                    txt_vi_tri.Text = objTin.Vi_tri.ToString();
                    anh_nho = objTin.Anh_nho;
                    anh_to = objTin.Anh_to;
                    txt_url.Text = objTin.Url;
                    //lay tin cha, loai tin
                    ListItem selecteditem=new ListItem();
                    selecteditem = drop_lt.Items.FindByValue(e.Item.Cells[1].Text);
                    if (selecteditem != null)
                        selecteditem.Selected = true;
                    layTinCha();
                    selecteditem = new ListItem();
                    selecteditem = drop_tin_cha.Items.FindByValue(e.Item.Cells[2].Text);
                    if (selecteditem != null)
                    {
                        selecteditem.Selected = true;
                    }
                    break;
                case "Delete":
                    Insert = false;
                    tin.deleteTin(strCA);
                    Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
                    break;
            }
        }




        void drop_loai_tin_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGrid();
        }


        private void BindGrid()
        {
            if (drop_loai_tin.SelectedValue != "")
            {
                //lay tin cap 1 cua loai tin
                Tin tin = new Tin();
                grid_tin.DataSource = tin.getAllTinType(drop_loai_tin.SelectedValue);
                grid_tin.DataBind();

            }
        }



        protected void AddButton_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = true;
            pnView.Visible = false;
            Insert = true;
            drop_lt.DataSource = lstObjLoaiTin;
            drop_lt.DataTextField = "ten_loai_tin";
            drop_lt.DataValueField = "loai_tin_id";
            drop_lt.DataBind();
            layTinCha();
            //reset text bõx
            cke_noi_dung.Text = "";
            cke_mo_ta_ngan.Text = "";
            txt_tieu_de.Text = "";
            txt_url.Text = "";
            txt_vi_tri.Text = "1";
            anh_to = "";
            anh_nho = "";

        }

        //check neu la menu ngang thi hien thi div_menu_ngang
        void layTinCha()
        {
            if (drop_lt.SelectedValue != Constants.MENU_NGANG && drop_lt.SelectedValue != Constants.RIGHT_MENU)
            {
                div_menu_ngang.Visible = false;
            }
            else
            {
                div_menu_ngang.Visible = true;
                if (drop_lt.SelectedValue != "")
                {
                    //lay all tin con
                    Tin tin = new Tin();
                    drop_tin_cha.DataSource = tin.getAllTinType(drop_lt.SelectedValue);
                    drop_tin_cha.DataTextField = "tieu_de";
                    drop_tin_cha.DataValueField = "tin_id";
                    drop_tin_cha.DataBind();
                    drop_tin_cha.Items.Insert(0, new ListItem("--chọn---", "0"));
                }

            }
        }

        void drop_lt_SelectedIndexChanged(object sender, EventArgs e)
        {
            layTinCha();
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            string sql = "";
            foreach (DataGridItem item in grid_tin.Items)
            {
                CheckBox checkBox = (CheckBox)item.Cells[0].Controls[1];

                if (checkBox.Checked == true)
                {
                    sql += "<Tin Tin_id=\"" + item.Cells[5].Text + "\" ></Tin>";
                }
            }
            Tin tin = new Tin();
            tin.deleteTinByXml(sql);
            Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
        }

        protected void RefreshButton_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void Update_Click(object sender, EventArgs e)
        {
            Utils utils = new Utils();
            ObjTin objTin = new ObjTin();
            Tin tin = new Tin();
            objTin.Loai_tin_id = int.Parse(drop_lt.SelectedValue);
            //loi 20130104
            //objTin.Tin_cha_id = int.Parse(drop_tin_cha.SelectedValue);

            //sua lai
            //
            if (drop_tin_cha.SelectedValue != "")
            {
                objTin.Tin_cha_id = int.Parse(drop_tin_cha.SelectedValue);
            }
            objTin.Url = txt_url.Text;
            objTin.Tieu_de = txt_tieu_de.Text;
            objTin.Tom_tat = cke_mo_ta_ngan.Text;
            objTin.Noi_dung = cke_noi_dung.Text;
            objTin.Vi_tri = int.Parse(txt_vi_tri.Text);
            String filename = DateTime.Now.ToString("ddmmyyhhmmss").ToString();
            objTin.Anh_nho = "";
            objTin.Anh_to = "";
            string temp;
            if (file_anh_nho.HasFile)
            {
                file_anh_nho.SaveAs(MapPath("/images/") + filename + utils.RemoveSign4VietnameseString(file_anh_nho.FileName));
                objTin.Anh_nho = filename + utils.RemoveSign4VietnameseString(file_anh_nho.FileName);
                if (anh_nho != "")
                {
                    temp = Server.MapPath("/images/" + anh_nho);
                    if (File.Exists(temp))
                    File.Delete(temp);
                }
                anh_nho = "";
            }
            //if (file_anh_to.HasFile)
            //{
            //    file_anh_to.SaveAs(MapPath("/images/") + filename + utils.RemoveSign4VietnameseString(file_anh_to.FileName));
            //    objTin.Anh_to = filename + utils.RemoveSign4VietnameseString(file_anh_to.FileName);
            //    if (anh_to != "")
            //    {
            //        temp = Server.MapPath("~/images/" + anh_to);
            //        if(File.Exists(temp))
            //        File.Delete(temp);
            //    }
            //    anh_to = "";
            //}
            
            
            objTin.Loai_tin_id = int.Parse(drop_lt.SelectedValue);
            //neu tao tin moi
            if (Insert)
            {
                tin.createTin(objTin);
                Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
            }
            //update
            else
            {

                objTin.Tin_id = int.Parse(Id);
                tin.updateTin(objTin);
                Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
            }
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            pnView.Visible = true;
            pnUpdate.Visible = false;
            Level = "";
            Insert = false;
        }
        protected void lbtUpdateOrd_Click(object sender, EventArgs e)
        {
            string sql = "";
            foreach (DataGridItem item in grid_tin.Items)
            {
                TextBox txt = (TextBox)item.FindControl("txtOrd");
                sql += "<Tin Tin_id=\"" + item.Cells[5].Text + "\" Vi_tri= \"" + txt.Text + "\" ></Tin>";
            }
            Tin tin = new Tin();
            tin.updateTinVitriByXml(sql);
            //reset lai grid
            BindGrid();
        }
    }
}